package cn.iocoder.yudao.module.pickbook.service.classmember;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pickbook.controller.admin.classmember.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.classmember.ClassMemberDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pickbook.dal.mysql.classmember.ClassMemberMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.pickbook.enums.ErrorCodeConstants.*;

/**
 * 班级成员 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ClassMemberServiceImpl implements ClassMemberService {

    @Resource
    private ClassMemberMapper classMemberMapper;

    @Override
    public Long createClassMember(ClassMemberSaveReqVO createReqVO) {
        // 插入
        ClassMemberDO classMember = BeanUtils.toBean(createReqVO, ClassMemberDO.class);

        // 手动生成 id
        long id = UUID.randomUUID().getLeastSignificantBits();
        classMember.setId(id);

        classMemberMapper.insert(classMember);

        // 返回
        return classMember.getId();
    }

    @Override
    public void updateClassMember(ClassMemberSaveReqVO updateReqVO) {
        // 校验存在
        validateClassMemberExists(updateReqVO.getId());
        // 更新
        ClassMemberDO updateObj = BeanUtils.toBean(updateReqVO, ClassMemberDO.class);
        classMemberMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassMember(Long id) {
        // 校验存在
        validateClassMemberExists(id);
        // 删除
        classMemberMapper.deleteById(id);
    }

    @Override
        public void deleteClassMemberListByIds(List<Long> ids) {
        // 删除
        classMemberMapper.deleteByIds(ids);
        }


    private void validateClassMemberExists(Long id) {
        if (classMemberMapper.selectById(id) == null) {
            throw exception(CLASS_MEMBER_NOT_EXISTS);
        }
    }

    @Override
    public ClassMemberDO getClassMember(Long id) {
        return classMemberMapper.selectById(id);
    }

    @Override
    public PageResult<ClassMemberDO> getClassMemberPage(ClassMemberPageReqVO pageReqVO) {
        return classMemberMapper.selectPage(pageReqVO);
    }

}